<?php
/**
 * Created By virus_cms
 * Author: Virus
 * Date: 2020/2/28
 * Time: 23:02
 */

namespace app\common\model\mysql;


use think\facade\Db;
use think\Model;

class Admin extends Model
{
    protected $autoWriteTimestamp = true;

    protected $hidden = ['update_time'];

    public function getStatusAttr($value)
    {
        return $value ? '开启' : '禁用';
    }

    public function getLastLoginTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    public function getCreateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * @param $username
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAdminByUsername($username)
    {
        if (empty($username)) {
            return false;
        }

        $where  = ['username' => trim($username)];
        $result = $this->where($where)->find()->getData();

        return $result;
    }

    /**
     * 根据id更新数据
     * @param $id
     * @param $data
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateById($id, $data)
    {
        $id = intval($id);
        if (empty($id) || empty($data) || !is_array($data)) {
            return false;
        }
        // 密码修改
        if (isset($data['password'])) {
            $admin_one        = $this->getAdminByUid($id);
            $data['password'] = md5(md5($data['password']).$admin_one['salt']);
        }
        $where = ['id' => $id];

        return $this->where($where)->save($data);
    }

    /**
     * 根据id获取用户信息
     * @param $id
     * @return array|bool|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getAdminByUid($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return false;
        }
        $where = ['id' => $id];

        return $this->where($where)->find()->getData();
    }

    public function getAllAdmins()
    {
        return $this->select();
    }

    /**
     * @param $group_id
     * @param $data
     * @return bool
     */
    public function addAdmin($group_id, $data)
    {
        Db::startTrans();
        try {
            $admin_id = $this->insertGetId($data);

            Db::name('auth_group_access')->insert(
                [
                    'uid'      => $admin_id,
                    'group_id' => $group_id,
                ]
            );
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }

        return true;
    }

    public function updateAdminAndGroupById($id, $group_id, $data)
    {
        Db::startTrans();
        try {
            Db::name('auth_group_access')->where('uid', $id)->update(['group_id' => $group_id]);
            $result = $this->where('id', $id)->save($data);
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }

        return $result;
    }

    public function deleteAdminAndAccessGroupById($id)
    {
        Db::startTrans();
        try {
            Db::name('auth_group_access')->where('uid', $id)->delete();
            $result = $this->where('id', $id)->delete();
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
        }

        return $result;
    }
}